<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 美妆课堂
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-08-06 17:05:36 +0800 (三, 2014-08-06) $
 * @version : $Id: beauty.php 126 2014-08-06 09:05:36Z spike $
 * @rev     : $Revision: 126 $
 */
class Beauty extends CI_Controller 
{  
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * [浏览]
     */
    public function browse()
    {
        $params = $this->input->get();
        if($params)
        {
            $this->load->model('Beauty_model');
            $list = $this->Beauty_model->browse_beauty($params, $params['page'], $params['size']);

            $this->load->library('pagination');
            $config['total_rows'] = $list['total'];
            $config['per_page']   = $list['size'];
            $config['suffix']     = '#data-table';

            $this->pagination->initialize($config); 
            $pagination = $this->pagination->create_links();
        }

        // 统计
        $this->load->dao('Beauty_dao');
        $total = $this->Beauty_dao->count(array(
            'is_delete' => UNDELETED
        ));

        $data = array(
            'default'       => $params ? $params : array(),
            'total'         => $total,
            'list'          => $list,
            'pagesize'      => $this->config->item('pagesize'),
            'beauty_status' => $this->config->item('beauty_status'),
            'pagination'    => $pagination,
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('beauty/browse.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [新增]
     */
    public function add()
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp('add_beauty', $params);

            $this->load->model('Beauty_model');
      
            $this->load->library('upload', array(
                'upload_path'   => FRONTEND_DATA_PATH.'beauty',
                'allowed_types' => 'jpg|jpeg|png|gif|bmp',
                'encrypt_name'  => TRUE,
                'overwrite'     => FALSE,
            ));

            //上传封面
            if( ! $this->upload->do_upload('cover_img'))
            {
                show_error('新建美妆课堂失败：封面图片上传失败。');
            }
            $upload = $this->upload->data();
            $params['cover_img'] = $upload['file_name'];

            $this->load->model('Beauty_model');
            $max_detail_img = Beauty_model::$MAX_DETAIL_IMG;

            for($i = 1; $i <= $max_detail_img; $i++)
            {
                $name = "detail_img_{$i}";
                if( $_FILES[$name]['name'] )
                {
                    if( ! $this->upload->do_upload($name))
                    {
                        show_error("新建美妆课堂失败：第{$i}张图片上传失败。");
                    }
                    $upload = $this->upload->data();
                    $params['detail_img'][] = $upload['file_name'];
                }
            }

            if( FALSE === $beauty_id = $this->Beauty_model->add_beauty($params) )
            {
                show_error($this->Beauty_model->get_error(), 200, '错误', 'error_general_lite');
            }

            temp('add_beauty', NULL);

            show_message(
                "新增美妆课堂成功！", 
                array(
                    '继续编辑美妆课堂'   => DOMAIN."beauty/add",
                    '查看新建的美妆课堂' => DOMAIN."beauty/edit/{$beauty_id}",
                ),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Beauty_model');
            $page_style    = $this->config->item('page_style');
            $data = array(
                'temp'          => temp('add_beauty'),
                'page_style'    => $page_style['LITE'],
                'max_detail_img'=> Beauty_model::$MAX_DETAIL_IMG,
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('beauty/add.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [编辑]
     */
    public function edit($sn = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            $this->load->library('upload', array(
                'upload_path'   => FRONTEND_DATA_PATH.'beauty',
                'allowed_types' => 'jpg|jpeg|png|gif|bmp',
                'encrypt_name'  => TRUE,
                'overwrite'     => FALSE,
            ));

            //上传封面
            if( $_FILES['cover_img']['name'] )
            {
                if( ! $this->upload->do_upload('cover_img'))
                {
                    show_error('编辑美妆课堂失败：封面图片上传失败。');
                }
                $upload = $this->upload->data();
                $params['cover_img'] = $upload['file_name'];
            }

            $this->load->model('Beauty_model');
            $max_detail_img = Beauty_model::$MAX_DETAIL_IMG;

            for($i = 1; $i <= $max_detail_img; $i++)
            {
                $name = "detail_img_{$i}";
                if( $_FILES[$name]['name'] )
                {
                    if( ! $this->upload->do_upload($name))
                    {
                        show_error("编辑美妆课堂失败：第{$i}张图片上传失败。");
                    }
                    $upload = $this->upload->data();
                    $params['detail_img'][] = $upload['file_name'];
                }
                elseif($params["{$name}_origin"])
                {
                    $params['detail_img'][] = $params["{$name}_origin"];
                }
            }

            $this->load->model('Beauty_model');
            if( FALSE === $this->Beauty_model->edit_beauty($sn, $params) )
            {
                show_error($this->Beauty_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑美妆课堂成功！", 
                array('查看编辑的美妆课堂' => DOMAIN."beauty/edit/{$sn}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Beauty_model');
            if( ! $beauty = $this->Beauty_model->get_beauty($sn, FALSE, TRUE))
            {
                show_error($this->Beauty_model->get_error(), 200, '错误', 'error_general_lite');
            }

            $page_style    = $this->config->item('page_style');
            $data = array(
                'max_detail_img'=> Beauty_model::$MAX_DETAIL_IMG,
                'beauty'        => $beauty,
                'page_style'    => $page_style['LITE'],
                'beauty_status' => $this->config->item('beauty_status'),
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('beauty/edit.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [改变状态]
     */
    public function change_status($sn = NULL)
    {
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Beauty_model');
        if( FALSE === $this->Beauty_model->change_beauty_status($sn, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Beauty_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }

    /**
     * [删除一张详情图片]
     */
    public function del_detail_img($sn = NULL)
    {
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Beauty_model');
        if( FALSE === $this->Beauty_model->del_detail_img($sn))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Beauty_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }


}
/* End of file */